/*
 * emu.c
 *
 *  Created on: 2012-04-11
 *      Author: Aco
 */

#include "SDL/SDL.h"
#include <stdio.h>
#include <stdlib.h>

#include "log.h"
#include "glcd/mpgfx.h"

int running = 0;

void _EMU_init()
{
	if(SDL_Init(SDL_INIT_VIDEO) != 0)
	{
		_EMU_log("Video init failed, reason : %d", SDL_GetError());
		SDL_Quit();
	}
	else
	{
		_EMU_log("EMU init ok");
		running = 1;
	}
}

void _EMU_tick()
{
	SDL_Event event = {0};

	while (SDL_PollEvent(&event))
	{
		switch (event.type)
		{
		case SDL_KEYDOWN:
			break;

		case SDL_KEYUP:
			break;

		case SDL_QUIT:
			running = 0;
			break;
		}
	}

	if(running)
	{
		_EMU_GFX_tick();
	}
	else
	{
		exit(0);
	}
}

void Delay_ms(int delay)
{
	if(delay < 10)
	{
		_EMU_tick();
		SDL_Delay(delay);
	}
	else
	{
		while(delay > 0)
		{
			_EMU_tick();
			SDL_Delay(10);
			delay -= 10;
		}
	}
}

void Delay2s()
{
	Delay_ms(2000);
}
